/*
 * @Descripttion:
 * @version:
 * @Author: MichaelHu
 * @Date: 2022-06-29 16:25:02
 * @LastEditors: MichaelHU hyghyg126@126.com
 * @LastEditTime: 2024-11-21 10:48:31
 */
package myimage

import (
	"image"
	"image/color"
)

/*
*
  - @description: rgb888转rgb565
  - @param {image.Image} img
  - @param {bool} high_low_flag true 高位在前，flash 低位在前
  - @param {byte} mode 输出扫描方向  1: 自左而右，自上而下, 2: 自右至左，自上而下, 3: 自左而右，自下而上, 4: 自右至左，自下而上
    mode=1  ↓---------->		mode=2	<----------↓		mode=3  ↑---------->		mode=4	<----------↑
    ↓---------->				<----------↓				↑---------->				<----------↑
    ↓---------->				<----------↓				↑---------->				<----------↑
  - @return {*}
*/
func Rgb888_to_rgb565(img image.Image, high_low_flag bool, mode byte) []byte {
	var (
		r uint32
		g uint32
		b uint32
	)
	buf := make([]byte, 0)

	x := img.Bounds().Dx()
	y := img.Bounds().Dy()
	for j := 0; j < y; j++ {
		for i := 0; i < x; i++ {
			r, g, b, _ = img.At(i, j).RGBA()
			// log.Println(byte(r>>8), byte(g>>8), byte(b>>8))
			r8 := byte(r>>8) >> 3
			g8 := byte(g>>8) >> 2
			b8 := byte(b>>8) >> 3

			rgb565 := (((uint16)(r8) << 11) | ((uint16)(g8) << 5) | ((uint16)(b8) << 0))
			h := (byte)(rgb565 >> 8)
			l := (byte)(rgb565 >> 0)
			if high_low_flag {
				buf = append(buf, h)
				buf = append(buf, l)
			} else {
				buf = append(buf, l)
				buf = append(buf, h)
			}
		}
	}
	return buf
}

func Rgb888_to_rgb565_with_alpha(img image.Image, high_low_flag bool, mode byte, fallbackColor color.Color) []byte {
	var (
		r, g, b, a uint32
	)
	buf := make([]byte, 0)

	x := img.Bounds().Dx()
	y := img.Bounds().Dy()
	for j := 0; j < y; j++ {
		for i := 0; i < x; i++ {
			r, g, b, a = img.At(i, j).RGBA()

			// 检查是否透明
			if a == 0 { // 完全透明
				// 使用指定的fallback颜色
				r, g, b, _ = fallbackColor.RGBA()
			}

			// 调整颜色深度
			r8 := byte(r>>8) >> 3
			g8 := byte(g>>8) >> 2
			b8 := byte(b>>8) >> 3

			// 合并成RGB565格式
			rgb565 := (((uint16)(r8) << 11) | ((uint16)(g8) << 5) | ((uint16)(b8) << 0))
			h := (byte)(rgb565 >> 8)
			l := (byte)(rgb565 & 0xFF)
			if high_low_flag {
				buf = append(buf, h)
				buf = append(buf, l)
			} else {
				buf = append(buf, l)
				buf = append(buf, h)
			}
		}
	}
	return buf
}

/*
*
  - @description: rgb888转rgb2
  - @param {image.Image} img
  - @param {byte} mode 输出扫描方向  1: 自左而右，自上而下, 2: 自右至左，自上而下, 3: 自左而右，自下而上, 4: 自右至左，自下而上
    mode=1  ↓---------->		mode=2	<----------↓		mode=3  ↑---------->		mode=4	<----------↑
    ↓---------->				<----------↓				↑---------->				<----------↑
    ↓---------->				<----------↓				↑---------->				<----------↑
  - @return {*}
*/
func Rgb888_to_rgb2(img image.Image, mode byte) []byte {
	var (
		r   uint32
		g   uint32
		b   uint32
		cnt byte
		bit byte
	)
	buf := make([]byte, 0)

	x := img.Bounds().Dx()
	y := img.Bounds().Dy()
	for j := 0; j < y; j++ {
		for i := 0; i < x; i++ {
			r, g, b, _ = img.At(i, j).RGBA()
			if byte(r>>8) == 255 && byte(g>>8) == 255 && byte(b>>8) == 255 {
				bit <<= 1
				bit += 0
				cnt++
			} else {
				bit <<= 1
				bit += 1
				cnt++
			}
			if cnt == 8 {
				buf = append(buf, bit)
				cnt = 0
				bit = 0
			}
		}
	}
	if cnt > 0 {
		buf = append(buf, bit)
	}
	return buf
}

func Rgb888_to_rgb8(img image.Image, mode byte) []byte {
	var (
		r uint32
		g uint32
		b uint32
	)
	buf := make([]byte, 0)

	x := img.Bounds().Dx()
	y := img.Bounds().Dy()
	for j := 0; j < y; j++ {
		for i := 0; i < x; i++ {
			r, g, b, _ = img.At(i, j).RGBA()
			r3 := byte(r>>8) >> 5
			g3 := byte(g>>8) >> 5
			b2 := byte(b>>8) >> 6

			rgb332 := (((uint16)(r3) << 5) | ((uint16)(g3) << 2) | ((uint16)(b2) << 0))

			buf = append(buf, byte(rgb332))
		}
	}
	return buf
}
